package com.dhcc.bpm.modules.metadata.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import org.springblade.core.mp.base.Excel;
import com.dhcc.bpm.common.utils.StringUtils;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.NullSerializer;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springblade.core.mp.base.BaseEntity;

/**
 * 接口定义数据表实体类
 *
 * @author ccab
 * @since 2020-06-03
 */
@Data
@TableName("opc_api_metadata")
@EqualsAndHashCode(callSuper = true)
@ApiModel(value = "ApiMetadata对象", description = "接口定义数据表")
public class ApiMetadata extends BaseEntity {

	private static final long serialVersionUID = 1L;

	/**
	* 名称
	*/
	@ApiModelProperty(value = "名称")
	@Excel(name = "名称")
	private String name;
	/**
	* 是否必填
	*/
	@ApiModelProperty(value = "是否必填")
	@Excel(name = "是否必填")
	private String isRequired;
	/**
	* 请求参数类型
	*/
	@ApiModelProperty(value = "请求参数类型")
	@Excel(name = "请求参数类型")
	private String inType; // path \ query
	/**
	* 类型
	*/
	@ApiModelProperty(value = "类型")
	@Excel(name = "类型")
	private String type;
	/**
	* 类型格式
	*/
	@ApiModelProperty(value = "类型格式")
	@Excel(name = "类型格式")
	private String typeFormat;
	/**
	* 默认值
	*/
	@ApiModelProperty(value = "默认值")
	@Excel(name = "默认值")
	private String defaultValue;
	/**
	* 枚举
	*/
	@ApiModelProperty(value = "枚举")
	@Excel(name = "枚举名称")
	private String fileEnum;
	/**
	* xml名称
	*/
	@ApiModelProperty(value = "xml名称")
	@Excel(name = "xml名称")
	private String xmlName;
	/**
	* 父id
	*/
	@JsonSerialize(
			using = ToStringSerializer.class,
			nullsUsing = NullSerializer.class
	)
	@ApiModelProperty(value = "父id")
	@Excel(name = "父id")
	private Long groupId;
	/**
	* 父名称
	*/
	@ApiModelProperty(value = "父名称")
	@Excel(name = "父名称")
	private String ref;
	@Excel(name = "是否xmlWrapped")
	private String xmlWrapped;
	/**
	* 描述
	*/
	@ApiModelProperty(value = "描述")
	@Excel(name = "描述")
	private String description;
	/**
	* 接口id
	*/
	@JsonSerialize(
			using = ToStringSerializer.class,
			nullsUsing = NullSerializer.class
	)
	@ApiModelProperty(value = "api接口id")
	@Excel(name = "api接口id")
	private Long apiId;
	/**
	* body类型
	*/
	@JsonSerialize(
			using = ToStringSerializer.class,
			nullsUsing = NullSerializer.class
	)
	@ApiModelProperty(value = "body类型")
	@Excel(name = "body类型")
	private Integer bodyType; //1 请求参数  2 响应参数

	/**
	 * 接口id
	 */
	 @JsonSerialize(
			using = ToStringSerializer.class,
			nullsUsing = NullSerializer.class
	)
	@ApiModelProperty(value = "api接口id")
	 @Excel(name = "元数据id")
	private Long metadataId;

	@ApiModelProperty(value = "描述")
	@Excel(name = "描述")
	private String describe;

	@ApiModelProperty(value = "字段长度")
	@Excel(name = "字段长度")
	private Integer length;

	@ApiModelProperty(value = "类型")
	private Integer busType;


	@Excel(name = "字段长度")
	@ApiModelProperty(value = "类型")
	private Integer orderField;
//	/**
//	 * 映射
//	 */
//	@ApiModelProperty(value = "映射")
//	private String fieldMapper;

	@ApiModelProperty(value = "sys接口id")
//	@Excel(name = "sys接口id")
	private Long sysId;
	@ApiModelProperty(value = "gateway接口id")
	//@Excel(name = "gateway接口id")
	private Long gatewayId;

	@ApiModelProperty(value = "baseProductId接口id")
	//@Excel(name = "gateway接口id")  BASE_PRODUCT_ID
	private Long baseProductId;
	@ApiModelProperty(value = "req+apiId 请求公共参数 res+apiId 响应公共参数")
	private String apiComData;

	public String toXml(String jname){
		StringBuilder stringBuilder =  new StringBuilder();
		stringBuilder.append(formate("id",this.getId().intValue()));
		stringBuilder.append(formate("name",name));
		stringBuilder.append(formate("jname",jname));
		stringBuilder.append(formate("type",type.equals("string")?"":type));
		if("list".equals(type)){
			stringBuilder.append(formate("datatype","object".equals(typeFormat)?typeFormat:""));
		}

		return stringBuilder.toString();
	}

	private String formate(String key,String value){

		if(StringUtils.isNotEmpty(value)){
			return String.format(" %s=\"%s\"",key,value);
		}else {
			return "";
		}
	}
	private String formate (String key,Integer value){
		if(0==value){
			return "";
		}else {
			return String.format(" %s=\"%s\"",key,value);
		}

	}
}
